package weaver.interfaces.geely.duss.action;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.weaver.integration.datesource.SAPInterationOutUtil;
import com.weaver.integration.log.LogInfo;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.geely.duss.utils.SapUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年11月28日 16:02
 */
public class WeaDynamicParaFypzAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        writeLog(">>>>>WeaDynamicParaFypzAction>>>>>requestid=" + requestid);
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaDynamicParaFypzAction>>>>>tablename=" + tablename);
        try {
            SapUtil sapUtil = new SapUtil();
            String type="1";
            RecordSet rs = new RecordSet();
            String message = "调用SAP系统接口返回:";
            String currentDate = DateUtil.getCurrentDate();
            rs.executeQuery("select id,bh,fypzbm,BUKRS,BLDAT,BUDAT,BLART,WAERS,BKTXT,TRADE_ID from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));//id
            String tablenameDtl = Util.null2String(rs.getString("fypzbm"));//id;
            String bh = Util.null2String(rs.getString("bh"));//编号
            String BUKRS = Util.null2String(rs.getString("BUKRS"));//公司代码
            String BLART = Util.null2String(rs.getString("BLART"));//凭证类型
            String WAERS = Util.null2String(rs.getString("WAERS"));//币种
            String BKTXT = Util.null2String(rs.getString("BKTXT"));//抬头文本
            String TRADE_ID = Util.null2String(rs.getString("TRADE_ID"));//贸易伙伴
            SAPInterationOutUtil outUtil = new SAPInterationOutUtil();
            JCO.Client myConnection = (JCO.Client) outUtil.getConnection(type, new LogInfo());
            myConnection.connect();
            // 实例名
            JCO.Repository myRepository = new JCO.Repository("Repository", myConnection);
            //设置BAPI函数
            IFunctionTemplate ft = myRepository.getFunctionTemplate("ZFI_ACC_DOCUMENT_CREATE");
            JCO.Function bapi = ft.getFunction();
            //输入参数和结构处理
            JCO.ParameterList input = bapi.getImportParameterList();
            input.setValue(bh, "IV_OANUM");
            //输入表的处理
            JCO.ParameterList inputtable = bapi.getTableParameterList();
            JCO.Table t1 = inputtable.getTable("ZPI_FIPOST");
            rs.executeQuery("select * from " + tablenameDtl + " where mainid=?", mainid);
            int counts = rs.getCounts();
            writeLog(">>>>>WeaDynamicParaFkpzAction>>>>>counts=" + counts);
            if (counts > 0) {
                while (rs.next()) {
                    t1.appendRow();
                    t1.setValue(mainid, "NUM");
                    t1.setValue(BUKRS, "BUKRS");
                    t1.setValue(currentDate.replace("-", ""), "BLDAT");
                    t1.setValue(currentDate.replace("-", ""), "BUDAT");
                    t1.setValue(BLART, "BLART");
                    t1.setValue(WAERS, "WAERS");
                    t1.setValue(BKTXT, "BKTXT");
                    t1.setValue(TRADE_ID, "TRADE_ID");
                    t1.setValue(Util.null2String(rs.getString("KOART")), "KOART");
                    t1.setValue(Util.null2String(rs.getString("SAKNR")), "HKONT");
                    t1.setValue(Util.null2String(rs.getString("DMBTR")), "DMBTR");
                    t1.setValue(Util.null2String(rs.getString("WRBTR")), "WRBTR");
                    String ZFBDT = Util.null2String(rs.getString("ZFBDT"));
                    t1.setValue(ZFBDT.replace("-", ""), "ZFBDT");
                    t1.setValue(Util.null2String(rs.getString("KOSTL")), "KOSTL");
                    t1.setValue(Util.null2String(rs.getString("AUFNR")), "AUFNR");
                    t1.setValue(Util.null2String(rs.getString("POSID")), "POSID");
                    t1.setValue(Util.null2String(rs.getString("ANBWA")), "ANBWA");
                    t1.setValue(Util.null2String(rs.getString("PRCTR")), "PRCTR");
                    t1.setValue(Util.null2String(rs.getString("ZUONR")), "ZUONR");
                    t1.setValue(Util.null2String(rs.getString("SGTXT")), "SGTXT");
                    t1.setValue(Util.null2String(rs.getString("MATNR")), "MATNR");
                    t1.setValue(Util.null2String(rs.getString("MENGE")), "MENGE");
                    t1.setValue(Util.null2String(rs.getString("MEINS")), "MEINS");
                    t1.setValue(Util.null2String(rs.getString("ZZCFTCCD")), "RSTGR");
                    t1.setValue(Util.null2String(rs.getString("XNEGP")), "XNEGP");
                    t1.setValue(Util.null2String(rs.getString("MWSKZ")), "MWSKZ");
                    t1.setValue(Util.null2String(rs.getString("FAPIAONO")), "FAPIAONO");
                    t1.setValue(Util.null2String(rs.getString("FAPIAOCODE")), "FAPIAOCODE");
                }
                writeLog(">>>>>WeaDynamicParaFypzAction>>>>>t1=" + t1);
                //执行函数
                myConnection.execute(bapi);
                //输出参数和结构处理
                JCO.ParameterList outPrint = bapi.getExportParameterList();
                String IV_BELNR = Util.null2String(outPrint.getValue("IV_BELNR"));
                if (!"".equals(IV_BELNR)) {
                    rs.executeUpdate("update " + tablename + " set fypz=? where requestid=?", IV_BELNR, requestid);
                }
                //输出表的处理
                JCO.ParameterList Table00 = bapi.getTableParameterList();
                //返回处理结果以及日志
                JCO.Table t = Table00.getTable("ZRETURN");
                writeLog(">>>>>WeaDynamicParaFypzAction>>>>>t=" + t);
                String TYPE = Util.null2String(t.getValue("TYPE"));
                String MESSAGE = Util.null2String(t.getValue("MESSAGE"));
                JCO.releaseClient(myConnection);
                if (!"S".equalsIgnoreCase(TYPE)) {
                    Map<String, Object> apilogMap = new HashMap<>();
                    apilogMap.put("jkmc", "WeaDynamicParaFypzAction");
                    apilogMap.put("jkqqcs", t1);
                    apilogMap.put("jkxycs", t);
                    apilogMap.put("jkqqdz", "ZFI_ACC_DOCUMENT_CREATE");
                    apilogMap.put("jkdysj", DateUtil.getFullDate());
                    apilogMap.put("jktgf", "SAP");
                    apilogMap.put("lcqqid", requestid);
                    sapUtil.insertModelData(apilogMap);
                    message += Util.null2String(MESSAGE);
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaDynamicParaFypzAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
